package com.ict.web.noteConfig;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.ict.common.constant.Constants;
import com.ict.common.core.redis.RedisCache;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.web.vo.CodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Random;

/**
 * 短信接口配置
 * @author xc
 */
@Component
public class NoteConfig {
    @Autowired
    private RedisCache redisCache;
    private static RedisCache redis;

    @PostConstruct
    public void init() {
        redis = redisCache;
    }
    private static final String APP_CODE="user_16528618286736037875";
    private static final String API_CODE="API_CODE_16568318551195933000";
    /**短信接口地址*/
    private static final String URL="http://23.99.215.138:21100/rest";

    public final static String TITLE = "【渝中区大数据发展应用中心】";
    public final static String EXPORTS = "专家您好！您有一个";
    public final static String TASK_PROJECT = "项目待接受的评审任务，点击以下链接，输入验证码";
    public final static String TASK_MASSAGE = "进行处理：";
    public final static String TASK_MASSAGE_URL = "http://113.207.106.123:8000/yzq_manage/acceptTaskLogin.html?";
    public final static String DEMONSTRATION_MASSAGE_TIME = "现场论证的时间为";
    public final static String DEMONSTRATION_MASSAGE = "，请准时参加。如果时间有冲突，请及时反馈。";

    public static void getNote(String content,String phoneNum){
        create(content,phoneNum);
    }
    /**
     * 调用短信接口
     * @param content
     * @param phoneNum
     * @return
     */
    public static String create(String content,String phoneNum){
        JSONObject note = new JSONObject();
        note.put("content", content);
        note.put("mobile", phoneNum);
        HashMap<String, String> headersUser = new HashMap<>();
        headersUser.put("appCode", APP_CODE);
        headersUser.put("apiCode", API_CODE);
        headersUser.put("apiVersion", "1.0.0");
        headersUser.put("format", "http+json");
        headersUser.put("Content-Type", "application/json");
        return HttpUtil.createPost(URL).addHeaders(headersUser).form(note).execute().body();
    }

    /**
     * 生成验证码
     * @return
     */
    public static CodeVo getCode(){
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String code = null;
        // 生成验证码
        code = getCheckCode();
        redis.setCacheObject(verifyKey, code);
        return new CodeVo(uuid,code);
    }
    /**
     * 随机四位数
     * @return
     */
    public static String getCheckCode() {
        String ZiMu = "1234567890";
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(ZiMu.length());
            char c = ZiMu.charAt(index);
            result += c;
        }
        return result;

    }
    /**
     * H5页面连接
     * @return
     */
    public static String createH5(String reviewTaskId,String uuid,String userId) {
        return TASK_MASSAGE_URL+"reviewTaskId="+reviewTaskId+"&uuid="+uuid+"&userId="+userId;
    }
}
